package com.itheima.mobilesafe69.db.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.itheima.mobilesafe69.db.BlackNumberOpenHelper;
import com.itheima.mobilesafe69.db.domain.BlackNumberInfo;


public class BlackNumberDao {
	//1,私有化构造方法
	private BlackNumberDao(Context ctx){
		//要在创建BlackNumberDao对象的过程中,就去生成数据库以及表,在此次创建BlackNumberOpenHelper对象
		mBlackNumberOpenHelper = new BlackNumberOpenHelper(ctx);
	};
	//2,创建一个对象
	private static BlackNumberDao mBlackNumberDao = null;
	private BlackNumberOpenHelper mBlackNumberOpenHelper;
	
	//3,对外提供一个返回创建对象的方法
	public static BlackNumberDao getInstance(Context ctx){
		if(mBlackNumberDao == null){
			mBlackNumberDao = new BlackNumberDao(ctx);
		}
		return mBlackNumberDao;
	}
	
	/**
	 * @param phone	拦截的电话号码
	 * @param mode  拦截类型
	 * 往数据库中插入一条数据
	 */
	public void insert(String phone,int mode){
		SQLiteDatabase db = mBlackNumberOpenHelper.getWritableDatabase();
		
		ContentValues contentValues = new ContentValues();
		contentValues.put("phone", phone);
		contentValues.put("mode", mode);

		db.insert("blacknumber", null, contentValues);
		
		db.close();
	}
	
	
	/**
	 * @param phone	根据电话号码删除数据
	 * 从数据库中删除一条数据
	 */
	public void delete(String phone){
		SQLiteDatabase db = mBlackNumberOpenHelper.getWritableDatabase();
		
		db.delete("blacknumber", "phone = ?", new String[]{phone});
		
		db.close();
	}
	
	/**
	 * @param phone		要去修改拦截类型的电话号码
	 * @param mode		修改的类型(1,短信,2电话,3,所有)
	 */
	public void update(String phone,int mode){
		SQLiteDatabase db = mBlackNumberOpenHelper.getWritableDatabase();
		
		ContentValues contentValues = new ContentValues();
		contentValues.put("mode", mode);
		
		db.update("blacknumber", contentValues, "phone = ?", new String[]{phone});
		
		db.close();
	}
	
	/**
	 * @return	查询数据库中所有数据,放置到集合中返回
	 */
	public List<BlackNumberInfo> findAll(){
		SQLiteDatabase db = mBlackNumberOpenHelper.getWritableDatabase();
		List<BlackNumberInfo> blackNumberList = new ArrayList<BlackNumberInfo>();
		
		//_id desc 按照自增长的id做逆序排列,最后添加的数据,在最前面显示
		Cursor cursor = db.query("blacknumber", new String[]{"phone","mode"}, null, null, null, null, "_id desc");
		
		while(cursor.moveToNext()){
			BlackNumberInfo blackNumberInfo = new BlackNumberInfo();
			blackNumberInfo.setPhone(cursor.getString(0));
			blackNumberInfo.setMode(cursor.getInt(1));
			
			blackNumberList.add(blackNumberInfo);
		}
		cursor.close();
		db.close();
		return blackNumberList;
	}
	
	
	public int findCount(){
		int count = 0;
		SQLiteDatabase db = mBlackNumberOpenHelper.getWritableDatabase();
		
		//_id desc 按照自增长的id做逆序排列,最后添加的数据,在最前面显示
		Cursor cursor = db.rawQuery("select count(*) from blacknumber;", null);
		
		if(cursor.moveToNext()){
			count = cursor.getInt(0);
		}
		cursor.close();
		db.close();
		return count;
	}
	
	/**
	 * @return	查询数据库中部分数据,查询条目数为20
	 */
	public List<BlackNumberInfo> find(int index){
		SQLiteDatabase db = mBlackNumberOpenHelper.getWritableDatabase();
		List<BlackNumberInfo> blackNumberList = new ArrayList<BlackNumberInfo>();
		
		
		//_id desc 按照自增长的id做逆序排列,最后添加的数据,在最前面显示
		Cursor cursor = db.rawQuery("select phone,mode from blacknumber order by _id desc limit ?,20;", new String[]{index+""});
		
		while(cursor.moveToNext()){
			BlackNumberInfo blackNumberInfo = new BlackNumberInfo();
			blackNumberInfo.setPhone(cursor.getString(0));
			blackNumberInfo.setMode(cursor.getInt(1));
			
			blackNumberList.add(blackNumberInfo);
		}
		cursor.close();
		db.close();
		return blackNumberList;
	}

	public int find(String phone) {
		SQLiteDatabase db = mBlackNumberOpenHelper.getWritableDatabase();
		int mode = -1;
		Cursor cursor = db.query("blacknumber", new String[]{"mode"}, "phone = ?", new String[]{phone}, null, null, null);
		if(cursor.moveToNext()){
			mode = cursor.getInt(0);
		}
		cursor.close();
		db.close();
		return mode;
	}
}
